/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.

*****************************************************************************************************/

/*globals Application, MessageBox, NMRSpectrum, qsTr, MnUi*/
/*jslint plusplus: true, indent: 4*/

function createArrayedSpectrum(aStacked) {
	"use strict";
	if (!Application.mainWindow.activeDocument) {
		MessageBox.critical(qsTr("No document found"));
		return;
	}
	var i, j, firstSpectrum, intersectingScale, scaleLimits, horizontalNucleus, spectraCount, currentSpectrum, newPage, newSpectrum,
		pagesArray = Application.mainWindow.activeDocument.selectedPages(),
		spectraArray = [];

	if (pagesArray.length) {
		firstSpectrum = new NMRSpectrum(pagesArray[0].item(0, "NMR Spectrum"));
		intersectingScale = firstSpectrum.getScaleLimits();
		horizontalNucleus = firstSpectrum.nucleus(1);
	} else {
		MessageBox.critical(qsTr("No spectra selected"));
		return;
	}

	for (i = 0; i < pagesArray.length; i++) {
		spectraCount = pagesArray[i].itemCount("NMR Spectrum");
		for (j = 0; j < spectraCount; j++) {
			currentSpectrum = new NMRSpectrum(pagesArray[i].item(j, "NMR Spectrum"));
			if (currentSpectrum.nucleus(1) === horizontalNucleus || currentSpectrum.nucleus(1) === "unknown") {
				spectraArray.push(currentSpectrum);
				scaleLimits = currentSpectrum.getScaleLimits();
				if (scaleLimits.fromX > intersectingScale.fromX) {
					intersectingScale.fromX = scaleLimits.fromX;
				}
				if (scaleLimits.toX < intersectingScale.toX) {
					intersectingScale.toX = scaleLimits.toX;
				}
			}
		}
	}

	if (spectraArray.length > 1) {
		if (intersectingScale.fromX < intersectingScale.toX) {
			newPage = Application.mainWindow.activeDocument.newPage();

			for (i = 0; i < spectraArray.length; i++) {
				newSpectrum = new NMRSpectrum(newPage.cloneItems(spectraArray[i]));
				newSpectrum.zoom(intersectingScale);
				newSpectrum.setProperty("axes.vertical.visible", false);

				if (aStacked) {
					if (i === 0) {
						newSpectrum.setProperty("axes.horizontal.visible", true);
					} else {
						newSpectrum.setProperty("axes.horizontal.visible", false);
					}
					newSpectrum.height = newPage.height / spectraArray.length;
					newSpectrum.width = newPage.width;
					newSpectrum.translate(newPage.left - newSpectrum.left, (newPage.bottom - newSpectrum.bottom) - (newSpectrum.height * i));
				} else {
					newSpectrum.height = newPage.height;
					newSpectrum.width = newPage.width;
					newSpectrum.translate(newPage.left - newSpectrum.left, newPage.top - newSpectrum.top);
				}
				newSpectrum.update();
			}
			Application.mainWindow.activeDocument.update();
		} else {
			MessageBox.critical(qsTr("Scales of selected spectra are not intersecting"));
		}
	} else {
		MessageBox.critical(qsTr("At least two spectra are needed"));
	}
}

if (this.MnUi && MnUi.scripts_nmr) {
	MnUi.scripts_nmr.scripts_nmr_OverlaySpectra = function () {
		"use strict";
		createArrayedSpectrum(false);
	};
	MnUi.scripts_nmr.scripts_nmr_StackSpectra = function () {
		"use strict";
		createArrayedSpectrum(true);
	};
}
